package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description NursingPlanServiceImpl
 * @Author lizhiwen
 * @Date 2024-09-09
 */
@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Override
    public List<NursingPlanVo> findAll() {
        List<NursingPlanVo> list = nursingPlanMapper.findAll();

        return list;
    }

    @Override
    public NursingPlanVo findById(Integer id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.findById(id);
        return nursingPlanVo;
    }
    @Transactional
    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setStatus(0);
        nursingPlan.setCreateBy(1L);
        nursingPlan.setCreateTime(LocalDateTime.now());
        nursingPlanMapper.add(nursingPlan);

        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(!projectPlans.isEmpty()){
            projectPlans.forEach(
                    projectPlan->projectPlan.setPlanId(nursingPlan.getId())
            );
            nursingProjectPlanMapper.add(projectPlans);
        }



    }

    @Override
    public PageResponse<NursingPlanVo> page(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> page = nursingPlanMapper.page(name, status);
        PageResponse<NursingPlanVo> of = PageResponse.of(page, NursingPlanVo.class);
        return of;
    }

    @Override
    public void deleteById(Integer id) {
        nursingPlanMapper.deleteById(id);

        nursingProjectPlanMapper.deleteById(id);
    }

    @Override
    public void setStatus(Integer id, Integer status) {
        nursingPlanMapper.setStatus(id, status);
    }
}
